<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\news\NewsType;
use app\common\model\news\News as NewsModel;
use think\Request;

class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function typeList(){
        $res=NewsType::getNewsTypeList();
        $this->success("新闻分类列表",$res);
    }

    public function newsList(Request $request){
        $field=$request->param("field")?:"id,news_title,news_published_time,news_description,news_image";
        $start_page=$request->param("page")?:1;
        $limit=$request->param("limit")?:3;
        $type=$request->param("news_type");
        if(empty($type)){
            $type=NewsType::where("pid",0)->field("id")->order("news_type_sort desc,id desc")->find()->toArray()['id'];
        }
        $res=NewsModel::getNewsList($type,$start_page,$limit,$field);
        $this->success("新闻列表",$res);
    }

    public function newsDetail(Request $request){
        $news_id=$request->param("news_id");
        $field=$request->param("field")?:"id,news_title,news_published_time,news_text";
        $res=NewsModel::getNewsDetail($news_id,$field);
        if($res!==false)
            $this->success("获取新闻详细内容成功",$res);
        $this->error("获取新闻详细内容失败");
    }
}